package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dao.CartDao;
import dao.CommentDao;
import dao.GoodsDao;
import dao.OrderDao;
import dao.Order_itemDao;
import entity.Cart;
import entity.Comment;
import entity.Goods;
import entity.Order;
import entity.Order_item;
import entity.User;
import net.sf.json.JSONArray;

/**
 * Servlet implementation class CheckoutServlet
 */
@WebServlet("/comment")
public class CommentServlet extends HttpServlet {
	private CartDao cartDao = new CartDao();
	private OrderDao orderDao = new OrderDao();
	private Order_itemDao o_itemDao = new Order_itemDao();
	private CommentDao commentDao = new CommentDao();
	private GoodsDao goodsDao = new GoodsDao();
	private Goods goods = new Goods();
	DecimalFormat df = new DecimalFormat("######0.00");
	Order order = new Order();
	Order_item o_item = new Order_item();
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		doPost(req, resp);
	}
	
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");//这是一个坑，前端传数据进来，要先判断它编码是否正确
		resp.setContentType("text/html;charset=UTF-8");// 防止中文乱码
		System.out.println("jjjj");
        // 获取Session中的用户uid
        HttpSession session = req.getSession();
        String ud = (String) session.getAttribute("uid");
        int uid = Integer.parseInt(ud);
        int gid;
        String act= req.getParameter("act");
        String gd= req.getParameter("gid");
        req.setAttribute("gid", gd);	
        
		SimpleDateFormat simpleDateFormat;
		simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date = simpleDateFormat.format(new Date());
		
        if(act.equals("1")) {
			String comment = req.getParameter("comment");
			gid =Integer.parseInt(gd);
			try {
				Comment com = new Comment();
				com=commentDao.queryByUGId(uid,gid);
			if(com != null) {
					com.setUid(uid);
					com.setGid(gid);
					com.setComment(comment);
					commentDao.updateComment(com);
					System.out.println("添加评论成功！");
				}else {
					com = new Comment();
					com.setUid(uid);
					com.setGid(gid);
					com.setComment(comment);
					com.setCtime(date);
					com.setComment(comment);
					
					commentDao.addComment(com);
					System.out.println("覆盖评论成功！");
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			Boolean a = true;
			// 把java数组转化成转化成json对象
			JSONArray jsonArray2 = JSONArray.fromObject(a);
			System.out.println(jsonArray2);
			PrintWriter out = resp.getWriter();
			out.print(jsonArray2); // 输出,返给ajax请求
			
		}else {
			req.getRequestDispatcher("comment.jsp").forward(req, resp);
		}

//        req.getRequestDispatcher("comment.jsp").forward(req, resp);
	}


}
